//下雪控制
function snow(){
   jSnow = $().jSnow({vSize:360,fadeAway:true,zIndex:65535,interval:36});
}
function stopSnow(){
   jSnow.kill();
}

$(function(){
	//懒加载
	if (navigator.platform == "iPad") return;
	$("img").not(".cycle img").lazyload({
		effect:"fadeIn",
		placeholder: "http://wwmm.me/wp-content/themes/wm/images/n.gif"
	});

	//焦点图
	if ( $("#slides").length > 0 ) {
		$("#slides").slides({
			play:7000,
			pause:500,
			slideSpeed:1200,
			hoverPause:true,
			animationStart:function(current){
				$("#slides .text").animate({bottom:-105},200);
				if(window.console&&console.log){
					console.log("animationStart on slide:",current);
				};
			},animationComplete:function(current){
				$("#slides .text").animate({bottom:0},500);
				if(window.console&&console.log){
					console.log("animationComplete on slide:",current);
				};
			},slidesLoaded:function(){
				$("#slides .text").animate({bottom:0},200);
			}
		});
	}

	//界面切换按钮
	if ( $("#StyleSwitch").length > 0 ) {
		$("#StyleSwitch .switch-btn[rel='"+ nowStyle +"']").hide();
		$(".switch-btn").click(function(){
			var s = this.getAttribute("rel");
			switchStylestyle(s);
			if (s == "xmas") snow();
			if (s == "style") stopSnow();
			$("#StyleSwitch .switch-btn").show();
			$(this).hide();
			return false;
		});
		if (nowStyle == "xmas") jSnow = $().jSnow({vSize:360,fadeAway:true,zIndex:65535,interval:36});
	}
	//跟随提示
	$(".hov-tip").aToolTip();
	$("a[rel='cboxElement']").colorbox({slideshow:true});
	//左侧浮层
	//$('<div id="LeftDiv"><a href="http://www.diaochapai.com/survey588966" target="_blank">浅爱小调查</a></div>').appendTo('body').fadeIn();
});

//搜索条
function SearchBar(){
	var formDom=$("#SearchBar"),inputDom=$("#SearchBar>.inpt");
	var searchtitle = "搜索";
	var tip = "请输入要搜索的文字...";
	var searchKey = inputDom.val();
	inputDom.val(searchKey?searchKey:searchtitle);
	inputDom.click(function(){
		if (inputDom.val() == searchtitle || inputDom.val() == tip){
			$(this).select();
		}
	});
	formDom.submit(function(){
		if (inputDom.val() == "" || inputDom.val() == searchtitle || inputDom.val() == tip){
			inputDom.val(tip);
			inputDom.select();
			return false;
		}
	});
}

//正文提示
function madein(e){if(typeof window.k=="undefined"){window.k=""}var e=e||event;if(e.keyCode==116) window.k="";window.k+=e.keyCode+",";if(window.k=="38,38,40,40,37,37,39,39,"){alert(unescape("%u9ED1%u9ED1%u5236%u9020"));window.k="";}}document.onkeydown=madein;

//提示后跳转
function SureDo(src,q){
	var ret;
	ret=confirm(q);
	if(ret!=false)window.location=src;
}

//加入收藏
function AddBookmark(title, url){
var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL'; 
if(jQuery.browser.msie){
		window.external.AddFavorite(url, title);
	}else if (document.all){
		window.external.addFavorite(url,title);
	}else if (window.sidebar){
		window.sidebar.addPanel(title, url, "");
	}else{
		alert("您可以尝试通过快捷键 Ctrl + D 加入到收藏夹"); 
	}
}

//设为首页
function SetHome(obj,url){
	try{
		obj.style.behavior='url(#default#homepage)';obj.setHomePage(url);
	}
	catch(e){
		if(window.netscape){
			try{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}
			catch(e){
				suredo("help.html#sethome","当浏览器无法设置首页，是否查看帮助信息？");
			}
			var prefs=Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',url);
		}
	}
}

//固定浮层
(function($){
	$.fn.followDIV = function(options){
		var defaults = {}
		var options = $.extend(defaults,options);
		this.each(function(){
			var obj = $(this), t = obj.offset().top;
			window.onscroll = function(){f(obj,t)};
			window.onresize = function(){f(obj,t)};
			
			function f(obj,t){
				var dst = $(document).scrollTop();
				if(($.browser.msie == true) && ($.browser.version == 6.0)){
					if(dst > t) obj.css({position: "absolute", top: dst - t});
				}else{
					if(dst > t) obj.css({position: "fixed", top: "-" & dst + "px"});
				}
				if(dst <= t) obj.css({position: "static", top: 0});
			}
		});
	}
})(jQuery);

// 切换
function switchStylestyle(styleName){
	$('link[@rel*=style][title]').each(function(i){
		this.disabled = true;
		if (this.getAttribute('title') == styleName) this.disabled = false;
	});
	createCookie('wwmm_style', styleName, 365);
}

// cookie functions
function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name){
	createCookie(name,"",-1);
}